type RegisterInstance<T = unknown> = {
  setOptions: (options: T) => void;
  setOpen: (open: boolean) => void;
};

const DEFAULT_ID = "defaultId";
class Register<T = unknown> {
  instances: Record<string, RegisterInstance<T>> = {};
  register = (instance: RegisterInstance<T>, id = DEFAULT_ID) => {
    this.instances[id] = instance;
  };
  unregister = (id = DEFAULT_ID) => {
    delete this.instances[id];
  };
  open = <D = T>(options: D, id = DEFAULT_ID) => {
    const instance = this.instances[id];
    if (!instance) {
      console.warn(`Instance with id ${id} does not exist.`);
      return;
    }
    instance.setOptions(options as unknown as T);
    instance.setOpen(true);
  };
}
export default Register;
